package com.mimaxueyuan.disruptor.demo3;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;

/**
 * @author: Kevin
 * @官网: 	www.mimaxueyuan.com
 * @Q Q群:	660567408
 * @Email:	mimaxueyuan@163.com
 * [每天进步一点点、人生带来大改变...]
 * [本代码对应视频地址:http://study.163.com/course/introduction/1004573050.htm]
 */
public class UserEventProducerWithTranslator {

	private final RingBuffer<UserEvent> ringBuffer;

	public UserEventProducerWithTranslator(RingBuffer<UserEvent> ringBuffer) {
		this.ringBuffer = ringBuffer;
	}

	/**
	 * 一个参数的EventTranslator
	 */
	private static final EventTranslatorOneArg<UserEvent, User> TRANSLATOR_ONE = new EventTranslatorOneArg<UserEvent, User>() {
		
		public void translateTo(UserEvent event, long sequence, User arg0) {
			event.setId(arg0.getId());
			event.setAddress(arg0.getAddress());
			event.setAge(arg0.getAge());
			event.setName(arg0.getName());
		}
	};
	
	public void onData(User user) {
		ringBuffer.publishEvent(TRANSLATOR_ONE, user);
	}
}
